package user

import (
	"chaowan/data_source"
	"chaowan/models"
	"chaowan/service"
	"github.com/gin-gonic/gin"
	"net/http"
	"time"
)

type VipgiftsBody struct {
	Uid    int64 `json:"uid" gorm:"uid"`
	Vip    int64 `json:"vip" gorm:"vip"`
	Dengji int64 `json:"dengji" gorm:"dengji"` // 等级
}

func Vipgiftsjprice(ctx *gin.Context) {
	request := VipgiftsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	vip := request.Vip

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var vips models.Vipgift
	data_source.Db.Where("dengji = ?", vip+1).First(&vips)

	if vips.ID == 0 {
		var vip1 models.Vipgift
		data_source.Db.Where("dengji = ?", vip).First(&vip1)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    vip1,
			"msg":     "ok",
		})
		return
	} else {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    vips,
			"msg":     "ok",
		})
		return
	}

}

func Vipgiftfive(ctx *gin.Context) {
	request := VipgiftsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	vip := request.Vip

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var vipinfo models.Vipgift
	data_source.Db.Where("dengji = ?", vip).First(&vipinfo)

	var vips []models.Vipgift
	data_source.Db.Order("dengji asc").Find(&vips)
	var zgvip int64
	for _, v := range vips {
		zgvip = v.Dengji
	}
	if len(vips) <= 5 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    vips,
			"msg":     "ok",
		})
		return
	} else {
		var viplist []models.Vipgift
		if vip < 5 {
			zdvip := 1
			zgvip1 := 5
			for _, v := range vips {
				if v.Dengji >= int64(zdvip) && v.Dengji <= int64(zgvip1) {
					viplist = append(viplist, v)
				}
			}
		} else {
			if zgvip == vip {
				zdvip := vip - 4
				for _, v := range vips {
					if v.Dengji >= zdvip && v.Dengji <= vip {
						viplist = append(viplist, v)
					}
				}
			} else {
				zdvip := vip - 3
				zgvip1 := vip + 1
				for _, v := range vips {
					if v.Dengji >= zdvip && v.Dengji <= zgvip1 {
						viplist = append(viplist, v)
					}
				}
			}
		}
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    viplist,
			"msg":     "ok",
		})
		return

	}

}
func Vipgifts(ctx *gin.Context) {
	request := VipgiftsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)
	var vips []models.Vipgift
	data_source.Db.Order("dengji asc").Find(&vips)
	var uservips []models.Uservipgift
	data_source.Db.Where("uid = ?", uid).Find(&uservips)

	for k, v := range vips {
		if v.Dengji == 1 {
			vips[k].Lingqu = 2
		} else {
			vips[k].Lingqu = 1
			if v.Dengji <= user.Vip {
				for _, u := range uservips {
					if v.Dengji == u.Dengji {
						vips[k].Lingqu = 2
						vips[k].Classify = u.Classify
						vips[k].Image = u.Image
						vips[k].Name = u.Name
						vips[k].Goodname = u.Goodname
						vips[k].Otherid = u.Otherid
					}
				}
			} else {
				vips[k].Lingqu = 3
			}
		}

	}
	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    vips,
		"msg":     "ok",
	})
	return
}
func Vipgiftinfo(ctx *gin.Context) {
	request := VipgiftsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	dengji := request.Dengji

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var vips models.Vipgift
	data_source.Db.Where("dengji = ?", dengji).First(&vips)
	if vips.Classify == 1 {
		var good models.Goods
		data_source.Db.Where("id = ?", vips.Otherid).First(&good)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    good,
			"msg":     "good",
		})
		return
	} else {
		var shangjuan models.Shangjuan
		data_source.Db.Where("id = ?", vips.Otherid).First(&shangjuan)
		ctx.JSON(http.StatusOK, gin.H{
			"code":    0,
			"error":   0,
			"success": 200,
			"data":    shangjuan,
			"msg":     "shangjuan",
		})
		return
	}

}

func Vipgiftlq(ctx *gin.Context) {
	request := VipgiftsBody{}
	ctx.ShouldBind(&request)
	uid := request.Uid
	dengji := request.Dengji

	if uid == 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    101,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "请登录",
		})
		return
	}
	var vips models.Vipgift
	data_source.Db.Where("dengji = ?", dengji).First(&vips)
	var user models.User
	data_source.Db.Where("id = ?", uid).First(&user)

	var uservipinfo models.Vipgift
	data_source.Db.Where("dengji = ?", dengji).Where("uid = ?", uid).First(&uservipinfo)
	if uservipinfo.ID > 0 {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "只能领取一次",
		})
		return
	}

	if user.Vip < dengji {
		ctx.JSON(http.StatusOK, gin.H{
			"code":    400,
			"error":   1,
			"success": 400,
			"data":    "on",
			"msg":     "等级不足无法领取",
		})
		return
	}

	uservip := models.Uservipgift{
		Dengji:   vips.Dengji,
		Classify: vips.Classify,
		Price:    vips.Price,
		Image:    vips.Image,
		Name:     vips.Name,
		Goodname: vips.Goodname,
		Otherid:  vips.Otherid,
		Uid:      uid,
	}
	data_source.Db.Create(&uservip)

	if vips.Classify == 1 {
		var good models.Goods
		data_source.Db.Where("id = ?", vips.Otherid).First(&good)
		service.Gooduser(user, good, 1, 5, 1, 0, 0)
	} else {
		var shangjuan models.Shangjuan
		data_source.Db.Where("id = ?", vips.Otherid).First(&shangjuan)
		if shangjuan.ID > 0 {
			var endtime int64
			addtime := time.Now().Unix()
			if shangjuan.Day > 0 {
				endtime = addtime + shangjuan.Day*864000
			}
			usershangjuan := models.Usershangjuan{
				Uid:         uid,
				Addtime:     addtime,
				Day:         shangjuan.Day,
				Name:        shangjuan.Name,
				Image:       shangjuan.Image,
				Daibi:       shangjuan.Daibi,
				Num:         shangjuan.Num,
				Userdaibi:   shangjuan.Userdaibi,
				Status:      1,
				Endtime:     endtime,
				Shangjuanid: shangjuan.ID,
				State:       3,
			}
			data_source.Db.Create(&usershangjuan)
		}

	}

	ctx.JSON(http.StatusOK, gin.H{
		"code":    0,
		"error":   0,
		"success": 200,
		"data":    vips,
		"msg":     "ok",
	})
	return
}
